package com.kepler.social.common.utils;

import com.google.common.collect.Maps;
import com.kepler.social.common.exception.ServiceException;
import com.kepler.social.common.exception.SystemException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Map;

public class ValidateAssert {

    private final static Logger LOGGER = LoggerFactory.getLogger(ValidateAssert.class);

    public static final Map<Class, Constructor> ccMap = Maps.newHashMap();

    static {
        try {
            ccMap.put(ServiceException.class, ServiceException.class.getDeclaredConstructor(String.class));
            ccMap.put(IllegalArgumentException.class, IllegalArgumentException.class.getDeclaredConstructor(String.class));
        } catch (ReflectiveOperationException e) {
            LOGGER.error("ReflectiveOperationException {}", e, e);
        }
    }


    public static <T extends RuntimeException> void newInstanceThenThrow(Class<T> clazz, String cv) {
        try {
            throw (T) ccMap.get(clazz).newInstance(new Object[]{cv});
        } catch (NullPointerException e) {
            LOGGER.error("ReflectiveOperationException {}", e, e);
            throw new SystemException("初始化失败");
        } catch (ReflectiveOperationException e) {
            LOGGER.error("ReflectiveOperationException {}", e, e);
            throw new SystemException("生成对应的异常");
        }
    }

    public static <T extends RuntimeException> void isTrue(boolean expression, String errorMsg, Class<T> clazz) {
        if (!expression) {
            newInstanceThenThrow(clazz, errorMsg);
        }
    }


    public static <T extends RuntimeException> void notNull(Object object, String errorMsg, Class<T> clazz) {
        if (object == null) {
            newInstanceThenThrow(clazz, errorMsg);
        }
    }


    public static void isTrue(boolean expression, String errorMsg) {
        if (!expression) {
            throw new IllegalArgumentException(errorMsg);
        }
    }

    public static <T> T notNull(T object, String errorMsg) {
        if (object == null) {
            throw new IllegalArgumentException(errorMsg);
        } else {
            return object;
        }
    }

    public static void hasLength(String text, String errorMsg) {
        if (!StringUtils.hasLength(text)) {
            throw new IllegalArgumentException(errorMsg);
        }
    }

    public static void notEmpty(Object[] array, String errorMsg) {
        if (ObjectUtils.isEmpty(array)) {
            throw new IllegalArgumentException(errorMsg);
        }
    }

    public static void notEmpty(Collection collection, String errorMsg) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new IllegalArgumentException(errorMsg);
        }
    }

    public static void notEmpty(Map map, String errorMsg) {
        if (CollectionUtils.isEmpty(map)) {
            throw new IllegalArgumentException(errorMsg);
        }
    }

    public static void notEmpty(String str, String errorMsg) {
        if (StringUtils.isEmpty(str)) {
            throw new IllegalArgumentException(errorMsg);
        }
    }
}
